var pageNum = null;
var pageSize = 10;
let vm = new Vue({
    el: '.main-content',
    data: function () {
        return {
            pageInfo: {},
            condition: {
                name: '',
                id:''
            },
            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "parentId"
                    },

                },
                callback: {
                    onClick: this.onClick,
                },

                edit: {
                    enable: true,
                    removeTitle: "删除",
                    renameTitle: "修改"
                }
            },
            nodes: []
        }
    },

    methods: {

        getPage: function (pageNum, pageSize) {
            selectPage(pageNum, pageSize)
        },

        selectPage2: function (pageNum = 1, pageSize = 10) {
            axios({
                url: `/guguanjia/resource/selectPage/${pageNum}/${pageSize}`,
                params: this.condition
            }).then(response => {
                vm.$data.pageInfo = response.data;
            }).catch(error => {
                console.log(error.message)
            })
        },

        selectAll: function () {
            this.condition.id = '';
            this.condition.name = '';
            this.selectPage2()
        },

        toUpdate: function (id) {
            layer.open({
                type: 2,
                skin: 'layui-layer-demo', //样式类名
                title: '编辑',
                closeBtn: 0, //不显示关闭按钮
                anim: 2,
                area: ['80%', '80%'],
                shadeClose: true, //开启遮罩关闭
                content: '/guguanjia/resource/toUpdate',
                success: function (aa, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.abcdefgh(id)
                },
                end: function () {
                    selectPage(pageNum, pageSize);
                }
            })

        },

        detail: function (id) {
            layer.open({
                type: 2,
                skin: 'layui-layer-demo', //样式类名
                title: '编辑',
                closeBtn: 1, //不显示关闭按钮
                anim: 2,
                area: ['80%', '60%'],
                shadeClose: true, //开启遮罩关闭
                content: '/guguanjia/resource/detail',
                success: function (aa, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.abcdefgh(id)
                },
                end: function () {
                    selectPage(pageNum, pageSize);
                }
            })

        },

        doDelete: function (resource) {
            layer.msg('是否删除', {
                time: 20000, //20s后自动关闭
                btn: ['是', '否'],
                yes: () => {
                    //删除标记（0：正常；1：删除；2：审核；）
                    resource.delFlag = "1";
                    axios({
                        url: "/guguanjia/resource/updateById",
                        method: "POST",
                        data: resource,
                    }).then(response => {
                        layer.msg("删除成功")
                        this.getPage(pageNum, pageSize)

                    }).catch(error => {
                        layer.msg(error.message)
                    });
                }
            });
        },

        initTree: function () {
            axios({
                url: "/guguanjia/resource/findAll"
            }).then(resp => {
                this.nodes = resp.data;
                this.nodes[this.nodes.length] = {"id": 0, "name": "菜单列表"};
                $.fn.zTree.init($("#treeMenu"), this.setting, this.nodes);
            }).catch(error => {
                layer.msg(error.message)
            })
        },
        onClick: function (event, treeId, treeNode) {
            if (treeNode.id == 0) {
                this.condition.id = null;
                window.location.reload();
            } else {
                this.condition.id = treeNode.id;
                this.condition.name = "";
                this.selectPage2()
            }
        },

    },

    created: function () {
        selectPage(1, 10);
        this.initTree();
    }

});

function selectPage(pageNum, pageSize) {
    this.pageNum = pageNum;
    this.pageSize = pageSize;
    axios({
        url: `/guguanjia/resource/selectPage/${pageNum}/${pageSize}`,
    }).then(response => {
        vm.$data.pageInfo = response.data;
    }).catch(error => {
        layer.msg(error.message)
    })
}